let dataSource = {
    "水库": new Cesium.CustomDataSource('水库'),
    "地质灾害隐患点": new Cesium.CustomDataSource('地质灾害隐患点'),
    "漫水桥(路)": new Cesium.CustomDataSource('漫水桥(路)'),
    "危房": new Cesium.CustomDataSource('危房'),
    "旅游景区景点": new Cesium.CustomDataSource('旅游景区景点'),
    "易淹易涝点(区)": new Cesium.CustomDataSource('易淹易涝点(区)'),
    "渔船": new Cesium.CustomDataSource('渔船'),
    "渔港": new Cesium.CustomDataSource('渔港'),
    "塔吊": new Cesium.CustomDataSource('塔吊'),
    "其他": new Cesium.CustomDataSource('其他'),
    "三防责任人信息": new Cesium.CustomDataSource('三防责任人信息'),
};
const image = {
    "水库": "./images/sjzx/icons/水库.png",
    "地质灾害隐患点": "./images/sjzx/icons/地质灾害隐患点.png",
    "漫水桥(路)": "./images/sjzx/icons/漫水桥(路).png",
    "危房": "./images/sjzx/icons/危房.png",
    "旅游景区景点": "./images/sjzx/icons/旅游景区景点.png",
    "易淹易涝点(区)": "./images/sjzx/icons/易淹易涝点(区).png",
    "渔船": "./images/sjzx/icons/渔船.png",
    "渔港": "./images/sjzx/icons/渔港.png",
    "塔吊": "./images/sjzx/icons/塔吊.png",
    "其他": "./images/sjzx/icons/其他.png",
    "三防责任人信息": "./images/sjzx/icons/三防责任人信息.png"
};
function addPoints(pointType, pointData, kbApiDetail, eventType, event) {
    
    pointData.forEach(point => {
        let currentId = point.id ? (point.id + pointType) : (point.name + pointType)
        // 更新数据时使用
        let entity = viewer.entities.getById(currentId);
        if (entity) {
            return
            // entity.position = Cesium.Cartesian3.fromDegrees(Number(car.lon00306599), Number(car.lat00306598), 0);
        } else {
            let x = null;
            let y = null;
            if(point.sfkx=="是"){
                if (point.lon && point.lat) {
                    x = point.lon
                    y = point.lat
                } else if (point.lng && point.lat) {
                    x = point.lng
                    y = point.lat
                } else if (point.jd && point.wd) {
                    x = point.jd
                    y = point.wd
                }
                //1，市，2。县区，3，乡镇街道 3，村
                let labelDistance = {
                    "1": [70000, 50000],
                    "2": [50000, 30000],
                    "3": [30000, 18000],
                    "4": [18000, 0],
                }
                //console.log(labelDistance[point.distance][1], labelDistance[point.distance][0])
                let bill = viewer.entities.add({
                    id: point.id ? (point.id+pointType) : (point.name+pointType),//渔船没有id,其他都是id
                    detailId:point.id ? point.id: point.name,
                    position: Cesium.Cartesian3.fromDegrees(Number(x), Number(y), 0),
                    billboard: {
                        image: image[pointType], // default: undefined
                        horizontalOrigin: Cesium.HorizontalOrigin.CENTER, // default
                        verticalOrigin: Cesium.VerticalOrigin.BOTTOM, // default: CENTER
                        // color : Cesium.Color.LIME, // default: WHITE
                        width: 40,
                        height: 40,
                        heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                        scaleByDistance: new Cesium.NearFarScalar(80000, .7, 1000000, 0.5),
                        // distanceDisplayCondition: point.distance ? new Cesium.DistanceDisplayCondition(labelDistance[point.distance][1], labelDistance[point.distance][0]) : new Cesium.DistanceDisplayCondition(0)
                    },
                    type: pointType,//防汛防风类型
                    kbApiDetail,
                    // label: {
                    //     text: point.name ? point.name : point.charge,
                    //     show: true,
                    //     font: '15pt Source Han Sans CN', //字体样式
                    //     fillColor: Cesium.Color.LAWNGREEN, //字体颜色
                    //     outlineColor: Cesium.LabelStyle.BLACK,
                    //     style: Cesium.LabelStyle.FILL_AND_OUTLINE, //label样式
                    //     outlineWidth: 4,
                    //     // heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                    //     verticalOrigin: Cesium.VerticalOrigin.TOP, //垂直位置
                    //     horizontalOrigin: Cesium.HorizontalOrigin.CENTER, //水平位置
                    //     pixelOffset: new Cesium.Cartesian2(0, 0), //偏移
                    //     // distanceDisplayCondition: point.distance ? new Cesium.DistanceDisplayCondition(labelDistance[point.distance][1], labelDistance[point.distance][0]) : new Cesium.DistanceDisplayCondition(0, 6000),
                    // },
                });
                //添加点击事件
                if (eventType && event) {
                    bill[eventType] = event
                }
                entitiesArr.push(bill)
            }else{
                return
            }
        }
        
    });
}
function addNewPoints(pointType, pointData, kbApiDetail, eventType, event) {
    
    pointData.forEach(point => {
        let currentId = point.id ? (point.id + pointType) : (point.name + pointType)
        // 更新数据时使用
        let entity = viewer.entities.getById(currentId);
        if (entity) {
            return
            // entity.position = Cesium.Cartesian3.fromDegrees(Number(car.lon00306599), Number(car.lat00306598), 0);
        } else {
            let x = null;
            let y = null;
            if(point.sfkx=="是"){
                if (point.lon && point.lat) {
                    x = point.lon
                    y = point.lat
                } else if (point.lng && point.lat) {
                    x = point.lng
                    y = point.lat
                } else if (point.jd && point.wd) {
                    x = point.jd
                    y = point.wd
                }
                //1，市，2。县区，3，乡镇街道 3，村
                let labelDistance = {
                    "1": [70000, 50000],
                    "2": [50000, 30000],
                    "3": [30000, 18000],
                    "4": [18000, 0],
                }
                //console.log(labelDistance[point.distance][1], labelDistance[point.distance][0])
                let bill = viewer.entities.add({
                    id: point.id ? (point.id+pointType) : (point.name+pointType),//渔船没有id,其他都是id
                    detailId:point.id ? point.id: point.name,
                    position: Cesium.Cartesian3.fromDegrees(Number(x), Number(y), 0),
                    billboard: {
                        image: image[pointType], // default: undefined
                        horizontalOrigin: Cesium.HorizontalOrigin.CENTER, // default
                        verticalOrigin: Cesium.VerticalOrigin.BOTTOM, // default: CENTER
                        // color : Cesium.Color.LIME, // default: WHITE
                        width: 40,
                        height: 40,
                        heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                        scaleByDistance: new Cesium.NearFarScalar(80000, .7, 1000000, 0.5),
                        // distanceDisplayCondition: point.distance ? new Cesium.DistanceDisplayCondition(labelDistance[point.distance][1], labelDistance[point.distance][0]) : new Cesium.DistanceDisplayCondition(0)
                    },
                    type: pointType,//防汛防风类型
                    kbApiDetail,
                    // label: {
                    //     text: point.name ? point.name : point.charge,
                    //     show: true,
                    //     font: '15pt Source Han Sans CN', //字体样式
                    //     fillColor: Cesium.Color.LAWNGREEN, //字体颜色
                    //     outlineColor: Cesium.LabelStyle.BLACK,
                    //     style: Cesium.LabelStyle.FILL_AND_OUTLINE, //label样式
                    //     outlineWidth: 4,
                    //     // heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                    //     verticalOrigin: Cesium.VerticalOrigin.TOP, //垂直位置
                    //     horizontalOrigin: Cesium.HorizontalOrigin.CENTER, //水平位置
                    //     pixelOffset: new Cesium.Cartesian2(0, 0), //偏移
                    //     // distanceDisplayCondition: point.distance ? new Cesium.DistanceDisplayCondition(labelDistance[point.distance][1], labelDistance[point.distance][0]) : new Cesium.DistanceDisplayCondition(0, 6000),
                    // },
                });
                //添加点击事件
                if (eventType && event) {
                    bill[eventType] = event
                }
                entitiesArr.push(bill)
            }else{
                return
            }
        }
        
    });
}
function removePointBySign(typeName) {

    for (var i = 0; i < entitiesArr.length; i++) {

        if (entitiesArr[i].type === typeName) {
            viewer.entities.remove(entitiesArr[i]);
          
            entitiesArr.splice(i, 1);
            i--;
        }
    }
}
function removeAll() {
    for (var i = 0; i < entitiesArr.length; i++) {
        viewer.entities.remove(entitiesArr[i]);
        entitiesArr.splice(i, 1);
        i--;
    }
}
function createPoint(pointType, pointData, kbApiDetail, eventType, event) {
    //首次加载会慢  再加载就会很快
    if (!viewer.dataSources.contains(dataSource[pointType])) {
        viewer.dataSources.add(dataSource[pointType]);
    }
    let entities = dataSource[pointType].entities;
    //let 
    pointData.forEach(point => {
        // 更新数据时使用
        // let entity = entities.getById(car.vehicleno10000034);
        // if (entity) {
        //     entity.position = Cesium.Cartesian3.fromDegrees(Number(car.lon00306599), Number(car.lat00306598), 0);
        // } else {
        let x = null
        let y = null
        if (point.lon && point.lat) {
            x = point.lon
            y = point.lat
        } else if (point.lng && point.lat) {
            x = point.lng
            y = point.lat
        } else if (point.jd && point.wd) {
            x = point.jd
            y = point.wd
        }
        //1，市，2。县区，3，乡镇街道 3，村
        let labelDistance = {
            "1": [70000, 50000],
            "2": [50000, 30000],
            "3": [30000, 18000],
            "4": [18000, 0],
        }
        //console.log(labelDistance[point.distance][1], labelDistance[point.distance][0])
        let bill = entities.add({
            id: point.id ? point.id : point.name,//渔船没有id,其他都是id
            position: Cesium.Cartesian3.fromDegrees(Number(x), Number(y), 0),
            billboard: {
                image: image[pointType], // default: undefined
                horizontalOrigin: Cesium.HorizontalOrigin.CENTER, // default
                verticalOrigin: Cesium.VerticalOrigin.BOTTOM, // default: CENTER
                // color : Cesium.Color.LIME, // default: WHITE
                width: 40,
                height: 40,
                scaleByDistance: new Cesium.NearFarScalar(100000, 1, 1000000, 0.5),
                distanceDisplayCondition: point.distance ? new Cesium.DistanceDisplayCondition(labelDistance[point.distance][1], labelDistance[point.distance][0]) : new Cesium.DistanceDisplayCondition(0)
            },
            label: {
                text: point.name ? point.name : point.charge,
                show: true,
                font: '15pt Source Han Sans CN', //字体样式
                fillColor: Cesium.Color.LAWNGREEN, //字体颜色
                outlineColor: Cesium.LabelStyle.BLACK,
                style: Cesium.LabelStyle.FILL_AND_OUTLINE, //label样式
                outlineWidth: 4,
                // heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                verticalOrigin: Cesium.VerticalOrigin.TOP, //垂直位置
                horizontalOrigin: Cesium.HorizontalOrigin.CENTER, //水平位置
                pixelOffset: new Cesium.Cartesian2(0, 0), //偏移
                distanceDisplayCondition: point.distance ? new Cesium.DistanceDisplayCondition(labelDistance[point.distance][1], labelDistance[point.distance][0]) : new Cesium.DistanceDisplayCondition(0, 6000),
            },
            type: pointType,//防汛防风类型
            kbApiDetail
        });
        //添加点击事件
        if (eventType && event) {
            bill[eventType] = event
        }
        // }
    });
}
function removePointByType(type) {
    dataSource[type].entities.removeAll();
}

function removeAllPoint() {
    for (let key in dataSource) {
        dataSource[key].entities.removeAll();
    }
}

export default {
    createPoint,
    removePointByType,
    removeAllPoint,
    addPoints,
    removePointBySign,
    removeAll,
    addNewPoints
};
